<template>
    <div>
        <mHeader></mHeader>
        <div class="main-con">
            <div class="title">
                <p>{{ keyword }}</p>
                <div>搜索结果 {{ total }} 【<span :class="{ active: resultType == 'col' }" @click="choseResult('col')">专栏 {{
                    cols
                        }}</span> <span :class="{ active: resultType == 'art' }" @click="choseResult('art')">文库 {{ arts
                        }}</span>
                    <span :class="{ active: resultType == 'user' }" @click="choseResult('user')">人物 {{ users }}</span> 】
                </div>
            </div>
            <div class="rightMain">
                <!-- 用户 -->
                <div class="list_box" v-for="(item, index) in listData" :key="index"
                    @click="goPage(item.type, item.id)">
                    <div v-if="item.type == resultType">

                        <img :src="item.image" alt="">


                        <div class="list_t" v-if="item.type != 'user'">
                            <span class="line2">{{ item.name }}</span>

                            <div>
                                <span class="line1">{{ item.user_nickname }}</span>
                                <div class="eye">
                                    <img src="@/assets/img/eye.png" alt="">
                                    <span>{{ item.views }}</span>
                                </div>
                            </div>
                        </div>

                        <!-- <div class="list_t">
                            <span class="line2">{{ item.name }}</span>

                            <div>
                                <span class="line1">{{ item.user_nickname }}</span>
                                <div class="eye">
                                    <img src="@/assets/img/eye.png" alt="">
                                    <span>{{ item.views }}</span>
                                </div>
                            </div>
                        </div> -->

                        <div class="list_t2" v-else>
                            <div class="name vip">
                                <span class="line1 ">{{ item.user_nickname }}</span>
                                <img v-if="item.is_auth == 1" src="@/assets/img/vip.svg" alt="">
                            </div>
                            <span class="line1 sign">{{ item.sign }}</span>
                            <div class="hot">
                                <div>
                                    <img src="@/assets/img/gz.png" alt="">
                                    <span>{{ item.friends }}</span>
                                </div>
                                <div>
                                    <img src="@/assets/img/update.png" alt="">
                                    <span>{{ item.comments }}</span>
                                </div>
                                <div>
                                    <i class="iconfont">&#xec8c;</i>
                                    <span>{{ item.likes }}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <el-empty description="没有内容了" v-if="listData.length == 0"></el-empty>
            </div>
        </div>
    </div>
</template>
<script>
export default {

    data() {
        return {
            menuList: [],
            loading: false,
            totalPage: 1,
            pageNum: 1,
            list: [],
            total: 0,
            cols: 0,
            arts: 0,
            users: 0,
            resultType: 'col'

        };
    },
    computed: {
        listData() {
            let list = []
            this.list.forEach(i => {
                if (i.type == this.resultType) {
                    list.push(i)
                }
            })
            return list
        },
        path() {
            return this.$route.path
        },
        keyword() {
            return this.$route.query.keyword ? this.$route.query.keyword : ''
        }
    },
    watch: {
        keyword(n) {

            this.pageNum = 1
            this.list = []
            this.getList()
        },
    },
    mounted() {
        window.addEventListener('scroll', this.handleScroll); // 监听滚动事件
        this.getList()
    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll); // 移除监听
    },
    methods: {
        choseResult(a) {
            this.resultType = a
            this.pageNum = 1
            this.list = []
            this.getList()
        },
        goPage(type, id) {
            let url = '/columnIndex'
            if (type == 'col') {
                const route = this.$router.resolve({
                    path: url,
                    query: { id }
                });
                window.open(route.href, '_blank');
            }


            if (type == 'art') {
                let aid = id
                const route = this.$router.resolve({
                    path: '/articleRead',
                    query: { aid }
                });
                window.open(route.href, '_blank');
            }

            if (type == 'user') {
                const route = this.$router.resolve({
                    path: '/association/youping',
                    query: { id }
                });
                window.open(route.href, '_blank');
            }

        },
        getList() {
            this.loading = true
            let params = {
                page: this.pageNum,
                keyword: this.keyword,
            }

            this.$api.searchAll(params).then(res => {
                let response = res.data.data
                if (res.data.code == 1) {
                    console.log(res)
                    this.totalPage = response.list.last_page
                    this.list = this.list.concat(response.list.data);
                    this.total = response.list.total
                    this.cols = response.cols
                    this.arts = response.arts
                    this.users = response.users
                    this.loading = false
                }

            })
        },
        handleScroll() {
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const windowHeight = window.innerHeight;
            const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // 判断是否滚动到底部
            if (scrollTop + windowHeight >= scrollHeight - 20) {
                this.load()
            }
        },
        load() {
            var page = this.pageNum
            //如果请求未结束就返回不连续请求
            if (this.loading) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPage) {
                return false;
            }
            page = parseInt(page) + 1
            this.pageNum = page
            this.getList()
        },
        getTabtype(t, url) {
            this.curType = t
            if (this.path != url) this.$router.push(url)

        },
        onSubmit() {
            console.log('submit!', this.form);
        },

    },
}
</script>
<style lang="scss" scoped>
.main-con {
    flex-direction: column;

    .title {
        padding: 25px;
        display: flex;
        align-items: center;
        background-color: #F6F6F6;
        position: sticky;
        top: 98px;
        z-index: 22;

        p {
            font-weight: bold;
            font-size: 30px;
            color: #1068E7;
            margin-right: 30px;
        }

        >div {
            font-weight: 400;
            font-size: 20px;
            color: #333333;

            span {
                margin: 0 5px;
                cursor: pointer;
            }

            .active {
                text-decoration: underline;
                color: #1068E7;
            }
        }
    }
}

.rightMain {
    padding-top: 20px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 25px;

    .list_box {
        cursor: pointer;
        height: 130px;
        display: inline-flex;

        >div {
            display: inline-flex;
            align-items: center;
            width: 100%;
            height: 130px;
            border: 1px solid #C3DCFB;
            padding: 1rem 1.6rem 0.8rem 0.99rem;

            &:nth-child(3n-1) {
                margin: 10px 33px;
            }

            >img {
                width: 80px;
                height: 80px;
                border-radius: 50%;
                flex-shrink: 0;
            }

            .list_t {
                flex: 1;
                margin-left: 0.99rem;
                display: flex;
                justify-content: space-between;
                flex-direction: column;
                height: 100%;

                >span {
                    font-weight: 400;
                    font-size: 22px;
                    color: #000000;
                }

                >div {
                    display: flex;

                    align-items: center;
                    justify-content: space-between;

                    >span {
                        font-weight: 400;
                        font-size: 18px;
                        color: #999999;
                    }

                    .resou {
                        font-weight: 400;
                        font-size: 18px;
                        color: #999999;

                        >span {
                            color: #1068E7;
                        }
                    }

                    .eye {
                        font-weight: 400;
                        font-size: 18px;
                        color: #999999;
                        flex-shrink: 0;
                        display: flex;
                        align-items: center;

                        img {
                            width: 23px;
                            height: 16px;
                            margin-right: 0.3rem;
                        }
                    }
                }
            }

            .list_t2 {


                margin-left: 0.99rem;
                display: flex;
                justify-content: space-between;
                flex-direction: column;
                height: 100%;
                width: calc(100% - 100px);

                .name {
                    display: flex;
                    align-items: center;
                    font-weight: 400;
                    font-size: 1.15rem;
                    color: #333333;

                    img {
                        width: 1.25rem;
                        height: 1.25rem;
                        margin-left: 0.7rem;
                        flex-shrink: 0;
                    }
                }

                .vip {
                    color: #1068E7;
                }

                .sign {
                    width: 100%;
                    font-weight: 400;
                    font-size: 1.04rem;
                    color: #666666;
                }

                .hot {
                    display: flex;
                    align-items: center;
                    margin-top: 0.5rem;
                    justify-content: space-between;

                    div {
                        display: flex;
                        align-items: center;
                        font-weight: 400;
                        font-size: 0.94rem;
                        color: #999999;
                        margin-right: 1.6rem;

                        img {
                            width: 1.2rem;
                            height: 1.09rem;
                        }

                        &:nth-child(2) {
                            img {
                                width: 0.99rem;
                                height: 0.99rem;
                            }
                        }
                    }
                }
            }
        }
    }
}

@media screen and (max-width:768px) {
    .main-con {
        width: 100%;
        .title{
            p{
                font-size: 16px;
            }
            >div{
                font-size: 13px;
            }
        }
    }

    .rightMain {
        padding-top: 20px;
        display: grid;
        grid-template-columns: repeat(1, 1fr);
        gap: 10px;

        .list_box {
            margin: 0 0 10px !important;
            padding: 10px;
            width: 100%;
            height: 100px;

            >div {
                height: 100px;

                >img {
                    width: 70px;
                    height: 70px;
                }

                .list_t {
                    margin-left: 15px;

                    >span {
                        font-size: 16px;
                    }

                    >div {
                        >span {
                            font-size: 16px;
                        }

                        .eye {
                            font-size: 13px;

                            img {
                                width: 15px;
                                height: 9px;
                                margin-right: 5px;
                            }
                        }
                    }
                }
                .list_t2{
                    margin-left: 20px;
                    .name {
                    display: flex;
                    align-items: center;
                    font-weight: 400;
                    font-size: 15px;
                    color: #333333;

                    img {
                        width: 70px;
                        height: 70px;
                        flex-shrink: 0;
                    }
                }

                .vip {
                    color: #1068E7;
                }

                .sign {
                    width: 100%;
                    font-weight: 400;
                    font-size: 13px;
                    color: #666666;
                }

                .hot {
                    display: flex;
                    align-items: center;
                    margin-top: 0.5rem;
                    justify-content: space-between;

                    div {
                        display: flex;
                        align-items: center;
                        font-weight: 400;
                        font-size: 12px;
                        color: #999999;
                        margin-right: 20px;

                        img {
                            width:15px;
                            height: 15px;
                        }

                        &:nth-child(2) {
                            img {
                                width:15px;
                                height: 15px;
                            }
                        }
                        i{
                            font-size: 13px;
                        }
                    }
                }
                }
            }


        }
    }
}
</style>